using System.Collections;
using UnityEngine;
using FrameWork;
using UnityEngine.Rendering.Universal;

public class WorldLightManager : MonoBehaviour
{
    [Header("灯光设置")]
    [SerializeField] private Light2D globalLight;                               // 全局灯光

    [Header("时间设置")]
    [SerializeField] private TimeDataSO timeData;

    [Header("灯光参数")]
    [SerializeField] private Color dayColor = new Color(1f, 1f, 1f); // 白天颜色
    [SerializeField] private Color nightColor = new Color(0.2f, 0.2f, 0.2f);  // 夜晚颜色
    
    [Header("打雷效果")]
    [SerializeField] private bool isThunderstorm = false;                       // 是否是雷阵雨
    [SerializeField] private float thunderIntensity = 3.0f;                     // 闪电强度
    [SerializeField] private float minThunderDuration = 1.0f;                   // 闪电最小持续时间
    [SerializeField] private float maxThunderDuration = 3.0f;                   // 闪电最大持续时间
    [SerializeField] private float minThunderInterval = 20.0f;                  // 闪电最小间隔时间
    [SerializeField] private float maxThunderInterval = 60.0f;                  // 闪电最大间隔时间
    private float originalNightIntensity = 0.3f;                                // 闪电日初始灯光效果
    public float OriginalNightIntensity{set => originalNightIntensity = value;} // 闪电日初始灯光效果

    private void Awake()
    {
        // // 早上6点到下午7点：白天
        // if (timeData.CurrentHour >= 6f && timeData.CurrentHour < 19f)
        // {
        //     globalLight.color = dayColor;
        // }
        // else
        // {
        //     globalLight.color = nightColor;
        // }
    }

    private void Start()
    {
        // 开始协程，定期更新灯光
        // StartCoroutine(UpdateLight());
    }

    public void SetLightIntensity(float intensity)
    {
        globalLight.intensity = intensity;
    }

    private IEnumerator UpdateLight()
    {
        while (true)
        {
            // 获取当前小时
            int currentHour = timeData.CurrentHour;

            // 根据当前小时调整灯光颜色
            UpdateLightBasedOnHour(currentHour);

            // 每秒更新一次
            yield return new WaitForSecondsRealtime(1f);
        }
    }

    private void UpdateLightBasedOnHour(int hour)
    {
        // 早上6点到下午7点：白天
        if (hour >= 6 && hour < 19)
        {
            // 早上6点到下午7点，白天颜色
            globalLight.color = dayColor;
        }
        // 下午7点到晚上9点：天色逐渐变暗
        else if (hour >= 19 && hour < 21)
        {
            // 计算过渡比例
            float transition = (hour - 19f) / 2f;
            globalLight.color = Color.Lerp(dayColor, nightColor, transition);
        }
        // 晚上9点到凌晨4点：夜晚
        else if (hour >= 21 || hour < 4)
        {
            // 夜晚颜色
            globalLight.color = nightColor;
        }
        // 凌晨4点到早上6点：天色逐渐变亮
        else if (hour >= 4 && hour < 6)
        {
            // 计算过渡比例
            float transition = (hour - 4f) / 2f;
            globalLight.color = Color.Lerp(nightColor, dayColor, transition);
        }
    }
    
    /// <summary>
    /// 开始打雷效果
    /// </summary>
    public void StartThunderstorm()
    {
        isThunderstorm = true;
        StartCoroutine(ThunderEffect());
    }

    /// <summary>
    /// 停止打雷效果
    /// </summary>
    public void StopThunderstorm()
    {
        isThunderstorm = false;
        StopAllCoroutines();
        globalLight.intensity = originalNightIntensity;
    }

    /// <summary>
    /// 打雷效果协程
    /// </summary>
    private IEnumerator ThunderEffect()
    {
        while (isThunderstorm)
        {
            // 随机生成闪电间隔时间（20秒到60秒之间）
            float randomInterval = Random.Range(minThunderInterval, maxThunderInterval);
            yield return new WaitForSecondsRealtime(randomInterval);

            // 随机生成闪电持续时间（1秒到3秒之间）
            float randomDuration = Random.Range(minThunderDuration, maxThunderDuration);

            // 闪电效果
            globalLight.intensity = thunderIntensity;
            yield return new WaitForSecondsRealtime(randomDuration);
            globalLight.intensity = originalNightIntensity;
        }
    }
}